<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">
                <h:form>

                    <p:panel header="Institution Item Movement and Stock" >

                        <h:panelGrid columns="4" >
                            <h:outputLabel value="Item Distributor" ></h:outputLabel>
                            <p:autoComplete 
                                completeMethod="#{institutionController.completeSupplier}" 
                                var="dept" itemLabel="#{dept.name}" itemValue="#{dept}" 
                                forceSelection="true" value="#{itemController.instituion}"
                                converter="institutionConverter" >
                            </p:autoComplete>
                            <h:outputLabel value="Persentage" ></h:outputLabel>
                            <h:inputText value="#{pharmacyController.persentage}" />
                            <h:outputLabel value="From" ></h:outputLabel>
                            <p:calendar value="#{pharmacyController.fromDate}"
                                        pattern="dd MM yyyy hh mm a" ></p:calendar>
                            <h:outputLabel value="To" ></h:outputLabel>
                            <p:calendar value="#{pharmacyController.toDate}"
                                        pattern="dd MM yyyy hh mm a" ></p:calendar>
                            <h:outputLabel value="" ></h:outputLabel>
                            <p:commandButton ajax="false" value="Average By Date"
                                            actionListener="#{pharmacyController.averageByDatePercentage()}" ></p:commandButton>
                            <p:commandButton ajax="false" value="Average By Month"
                                            actionListener="#{pharmacyController.averageByMonthByPercentage()}" ></p:commandButton>
                        </h:panelGrid>

                        <p:dataGrid value="#{pharmacyController.stockAverages}" var="sA" columns="1">

                            #{sA.item.name}   #{sA.item.code}
                            <p:dataTable styleClass="noBorder" value="#{sA.institutionStocks}" var="ins">
                                <p:columnGroup type="header">
                                    <p:row>
                                        <p:column style="text-align: left;">
                                            <f:facet name="header">
                                                Department Name
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="header">
                                                Required Stock
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="header">
                                                Current Stock
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="header">
                                                Average Qty
                                            </f:facet>
                                        </p:column>
                                    </p:row>
                                </p:columnGroup>
                                <p:subTable  value="#{ins.depatmentStocks}" var="dep">

                                    <f:facet name="header">
                                        #{ins.institution.name}
                                    </f:facet> 

                                    <p:column style="text-align: left;">
                                        #{dep.department.name}
                                    </p:column>
                                    <p:column style="text-align: left;">
                                        <h:outputLabel value="#{dep.stock-dep.average}">       
                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                        </h:outputLabel> 
                                    </p:column>
                                    <p:column style="text-align: left;">
                                        <h:outputLabel value="#{dep.stock}">       
                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                        </h:outputLabel> 
                                    </p:column>
                                    <p:column style="text-align: left;">
                                        <h:outputLabel value="#{dep.average}"> 
                                            <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                        </h:outputLabel> 
                                    </p:column>
                                    <p:columnGroup type="footer">
                                        <p:row>
                                            <p:column footerText="Total "></p:column>   
                                            <p:column style="text-align: left;"
                                                      footerText="#{ins.institutionTotal-ins.institutionAverage}">
                                                <f:facet name="footer">
                                                    <h:outputLabel value="#{ins.institutionTotal-ins.institutionAverage}">
                                                        <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </f:facet>
                                            </p:column>
                                            <p:column style="text-align: left;"
                                                      footerText="#{ins.institutionTotal}">
                                                <f:facet name="footer">
                                                    <h:outputLabel value="#{ins.institutionTotal}">
                                                        <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </f:facet>
                                            </p:column>
                                            <p:column style="text-align: left;" 
                                                      footerText="#{ins.institutionAverage}">
                                                <f:facet name="footer">
                                                    <h:outputLabel value="#{ins.institutionAverage}">
                                                        <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </f:facet>
                                            </p:column>
                                        </p:row>
                                    </p:columnGroup>
                                </p:subTable>  
                                <p:columnGroup type="footer">
                                    <p:row>
                                        <p:column style="text-align: left;">
                                            <f:facet name="footer" >
                                                Total Institution Stock
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="footer">
                                                <h:outputLabel  value="#{sA.itemStockTotal-sA.itemAverageTotal}">
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </h:outputLabel>
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="footer">
                                                <h:outputLabel  value="#{sA.itemStockTotal}">
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </h:outputLabel>
                                            </f:facet>
                                        </p:column>
                                        <p:column style="text-align: left;">
                                            <f:facet name="footer">
                                                <h:outputLabel  value="#{sA.itemAverageTotal}">
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </h:outputLabel>
                                            </f:facet>
                                        </p:column>
                                    </p:row>
                                </p:columnGroup>

                            </p:dataTable>     
                            <hr/><hr/>
                        </p:dataGrid>
                    </p:panel>
                </h:form>


            </ui:define>


        </ui:composition>

    </h:body>
</html>
